package com.itheima.service.impl;

import com.itheima.mapper.CheckGroupMapper;
import com.itheima.mapper.SetmealMapper;
import com.itheima.pojo.CheckGroup;
import com.itheima.pojo.Setmeal;
import com.itheima.service.SetmealService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;



@Service
public class SetmealServiceImpl implements SetmealService {

    @Autowired
    private SetmealMapper setmealMapper;

    @Autowired
    private CheckGroupMapper checkGroupMapper;
    /**
     * 获取所有套餐信息
     * @return
     */
    @Override
    public List<Setmeal> getSetmeal() {
        //拿到所有套餐
        List<Setmeal> setmeals = setmealMapper.getSetmeal();
        setmeals = setCheckGroups(setmeals);
        return setmeals;
    }


    @Override
    public Setmeal findById(Integer id) {
        Setmeal setmeal = setmealMapper.findById(id);
        List<Setmeal> list = new ArrayList<>();
        list.add(setmeal);
        list = setCheckGroups(list);
        return list.get(0);
    }



    /**
     * 封装套餐对应的检查组信息
     * @param setmeals
     * @return
     */
    private List<Setmeal> setCheckGroups(List<Setmeal> setmeals){
        for (Setmeal setmeal : setmeals) {
            Integer id = setmeal.getId();
            //拿到套餐关联检查项的id
            List<Integer> list = checkGroupMapper.getCheckGroupsBySetmealId(id);

            //拿到所有相关检查项
            List<CheckGroup> checkGroups = new ArrayList<>();
            for (Integer i : list) {
                checkGroups.add(checkGroupMapper.getById(i));
            }

            //封装
            setmeal.setCheckGroups(checkGroups);
        }
        return setmeals;
    }
}
